package com.itheima.store.web.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.itheima.store.domain.BookBorrow;
import com.itheima.store.domain.BookBorrowItems;
import com.itheima.store.domain.PageBean;
import com.itheima.store.domain.SystemSetting;
import com.itheima.store.domain.User;
import com.itheima.store.service.BookBorrowItemsService;
import com.itheima.store.service.BookBorrowService;
import com.itheima.store.utils.BaseServlet;
import com.itheima.store.utils.BeanFactory;
import com.itheima.store.utils.UUIDUtils;

import net.sf.json.JSONArray;

/**
 * Servlet implementation class BookBorrowItemsServlet
 */
public class BookBorrowItemsServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	public String saveOrder(HttpServletRequest req, HttpServletResponse resp){
		User user = (User) req.getSession().getAttribute("existUser");
		BookBorrowService bbs = (BookBorrowService) BeanFactory.getBean("bookBorrowService");
		String user_id= user.getUser_id();
		String bookBorrow_ids = "";
		String book_ids = "";
		//Integer total = 0;
		Integer alltotal=0;
		List list = new ArrayList();
		try {
			 list = bbs.findAppByUserId(user_id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(int i=0;i<list.size();i++){
			BookBorrow bb = (BookBorrow) list.get(i);
			String bookBorrow_id = bb.getBookBorrowing_id();
			String book_id = bb.getBooks_id();
			Integer total = bb.getTotal();
			Integer inventory = bb.getInventory();
			Integer tots = inventory - total;//剩余库存
			//total = bb.getTotal();
			//更新图书库存
			try {
				bbs.updateBookTotal(book_id,tots,1);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			bookBorrow_ids = bookBorrow_ids+","+bookBorrow_id;
			book_ids = book_ids+","+book_id;
			alltotal += total;
		}
		if(bookBorrow_ids.startsWith(",")){
			bookBorrow_ids = bookBorrow_ids.substring(1, bookBorrow_ids.length());
		}
		if(book_ids.startsWith(",")){
			book_ids = book_ids.substring(1, book_ids.length());
		}
		try {
			resp.sendRedirect(req.getContextPath()+"/BookBorrowItemsServlet?method=save&bookBorrowing_id="+bookBorrow_ids+"&book_id="+book_ids+"&total="+alltotal+"");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public String save(HttpServletRequest req, HttpServletResponse resp){
		BookBorrowItemsService bbis = (BookBorrowItemsService) BeanFactory.getBean("bookBorrowItemsService");
		User user = (User) req.getSession().getAttribute("existUser");
		SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String bookBorrowing_id = req.getParameter("bookBorrowing_id");
		String book_id = req.getParameter("book_id");
		String total = req.getParameter("total");
		String bookBorrowItems_id = UUIDUtils.getUUID();
		Date createTime = Calendar.getInstance().getTime();
			 
		String time = sdf.format(createTime);
		try {
			createTime = sdf.parse(time);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String user_id = user.getUser_id();
		String user_name = user.getUser_name();
		BookBorrowItems bbi = new BookBorrowItems();
		bbi.setBookBorrowItems_id(bookBorrowItems_id);
		bbi.setBookBorrowing_id(bookBorrowing_id);
		bbi.setBook_id(book_id);
		bbi.setCount(Integer.parseInt(total));
		bbi.setCreateUserId(user_id);
		bbi.setCreateUserName(user_name);
		bbi.setCreateTime(createTime);
		String flag = "0";
		try {
			 flag = bbis.save(bbi);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if("1".equals(flag)){
			req.setAttribute("msg", "您的图书申请成功,请去图书管理员处取书！");
		}else{
			req.setAttribute("msg", "您的图书申请失败,请联系图书管理员！！！");
		}
		return "/jsp/success.jsp";
	}
	
	
	public String findByUserId(HttpServletRequest req, HttpServletResponse resp){
		BookBorrowItemsService bbis = (BookBorrowItemsService) BeanFactory.getBean("bookBorrowItemsService");
		User user = (User) req.getSession().getAttribute("existUser");
		String user_id = user.getUser_id();
		// 接收参数:					
		Integer currPage = Integer.parseInt(req.getParameter("currPage")==null?"1":req.getParameter("currPage"));
		// 调用业务层:
		PageBean<BookBorrowItems> pageBean = null;
		List<SystemSetting> list = null;
		try {
			pageBean = bbis.findByUserId(user_id,currPage);
			list = bbis.findSetting();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int rebowDays = list.get(0).getRebowDays();		
		req.setAttribute("pageBean", pageBean);
		req.setAttribute("rebowDays", rebowDays);
		
		return "/jsp/bookBorrItem_list.jsp";
	}
	
	
	public String findByOid(HttpServletRequest req, HttpServletResponse resp){
		String oids = req.getParameter("oids");
		BookBorrowService bbs = (BookBorrowService) BeanFactory.getBean("bookBorrowService");
		List<BookBorrow> list = null;
		try {
			 list = bbs.findByOid(oids);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//req.setAttribute("list", list);
		JSONArray jsonArray = JSONArray.fromObject(list);
		try {
			resp.getWriter().println(jsonArray.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
}
